package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CustomerOrdersMapper;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.utils.UserThreadLocalUtils;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeVo;
import io.micrometer.core.instrument.binder.BaseUnits;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private CustomerReservationMapper customerReservationMapper;


    /**
     * 预约取消数量
     * @param updateBy
     * @return
     */
    @Override
    public int getCount(Long updateBy) {

        return  customerReservationMapper.getCount(LocalDateTime.now().withHour(0).withMinute(0).withSecond(0),
                LocalDateTime.now().withHour(23).withMinute(59).withSecond(59),updateBy);
    }

    /**
     * 新增预约
     *
     * @param dto
     */
    @Override
    public void add(ReservationDto dto) {
        String userId = UserThreadLocalUtils.getUserId();

        int count = getCount(Long.valueOf(userId));

        if (count >= 3){
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }

        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(dto,reservation);
        reservation.setStatus(ReservationStatus.PENDING.getOrdinal());
        reservation.setCreateBy(Long.valueOf(userId));
        reservation.setUpdateBy(Long.valueOf(userId));
        reservation.setStatus(0);
        reservation.setCreateTime(LocalDateTime.now());
        customerReservationMapper.insert(reservation);

    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<TimeVo> getTime(LocalDateTime time) {
        LocalDateTime endTime = time.plusHours(24);
        return customerReservationMapper.getTime(time,endTime);
    }

    /**
     * 分页查询预约
     *
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<ReservationVo> listAll(Integer pageNum, Integer pageSize, Integer status) {
        String userId = UserThreadLocalUtils.getUserId();

        Page<ReservationVo> page = customerReservationMapper.listAll(status,userId);

        PageHelper.startPage(pageNum,pageSize);
        PageResponse<ReservationVo> pageResponse = new PageResponse<>();
        pageResponse.setPage(pageNum);
        pageResponse.setPageSize(pageSize);
        pageResponse.setTotal(page.getTotal());
        pageResponse.setRecords(page.getResult());

        return pageResponse;
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void removal(Long id) {
        Long userId = UserThreadLocal.getUserId();

        int count = getCount(userId);

        if (count >= 3){
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        Reservation reservation = customerReservationMapper.removal(id);
        if (reservation != null ){
            reservation.setStatus(ReservationStatus.CANCELED.getOrdinal());
            reservation.setUpdateTime(LocalDateTime.now());
            customerReservationMapper.update(reservation);
        }
    }


    @Scheduled(cron = "0 1/30 8-19 * * ?")
    public void executeTask(){
        customerReservationMapper.executeTask();
    }
}
